package com.trg.generaltool.service.rabbitmq.workqueue;

import com.rabbitmq.client.*;
import com.trg.generaltool.util.rabbitmq.RabbitmqUtil;

import java.io.IOException;

/**
 * @ClassName: WorkQueue
 * @Description:
 * @Author: tangrg
 * @Date: 2021/7/19 10:06
 * @Version: 1.0
 * @Return:
 **/
public class customer1 {

    public static void main(String[] args) throws Exception{
        customer1();
    }

    public static void customer1() throws IOException {
        Connection connect = RabbitmqUtil.getConnect();
        Channel channel = connect.createChannel();
        //当前通道只执行一个消息
        channel.basicQos(1);
        channel.queueDeclare("hello", true, false, false, null);
        channel.basicConsume("hello", false, new DefaultConsumer(channel) {
            @Override
            public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
                System.out.println("当前消费者一信息：" + new String(body));
                try {
                    Thread.sleep(2000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                //由于上面的autoAkc关闭了消息的自动确认，所以这里在消息消费完后，每次都要手动确认
                channel.basicAck(envelope.getDeliveryTag(), false);
            }
        });

    }
}
